<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:divth="http://www.w3.org/1999/xlink">
<head th:replace="commons/fragments::head('关于我')">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="../js/jquery.1.9.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.css">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
            integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
            crossorigin="anonymous"></script>
    <script>

        function clear() {
            $("#password").val("");
            $("#newpassword").val("");
            $("#twopassword").val("");
        }

        function checkPassWord() {
            var flag = false;
            var password = $("#password").val();
            if (password == null || password == "null" || password == "") {
                flag = false;
                $("#error_msg").html("密码不能为空");
                return;
            } else {
                var reg = /([a-zA-Z0-9!@#$%^&*()_?<>{}]){6,18}/;
                if (!reg.test(password)) {
                    flag = false;
                    $("#error_msg").html("密码6-18位包含字母数组下划线");
                    return;
                } else {
                    flag = true;
                }
            }
            return flag;
        }

        function checkNewPassWord() {
            var flag = false;
            var newpassword = $("#newpassword").val();
            if (newpassword == null || newpassword == "null" || newpassword == "") {
                flag = false;
                $("#error_msg").html("新密码不能为空");
                return;
            } else {
                var reg = /([a-zA-Z0-9!@#$%^&*()_?<>{}]){6,18}/;
                if (!reg.test(newpassword)) {
                    flag = false;
                    $("#error_msg").html("密码6-18位包含字母数组下划线");
                    return;
                } else {
                    flag = true;
                }
            }
            return flag;
        }

        function checkTwoPassWord() {
            var flag = false;
            var twopassword = $("#twopassword").val();
            if (twopassword == null || twopassword == "null" || twopassword == "") {
                flag = false;
                $("#error_msg").html("确认密码不能为空");
                return;
            } else {
                var newpassword = $("#newpassword").val();
                if (twopassword !== newpassword) {
                    flag = false;
                    $("#error_msg").html("两次密码不一致，请检查");
                    return;
                } else {
                    flag = true;
                }
            }
            return flag;
        }

        function checkPhoto() {
            var flag=false;
            $('#fileInp').on('change', function () {
                var filePath = $(this).val(), //获取到input的value，里面是文件的路径
                    fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase(),
                    src = window.URL.createObjectURL(this.files[0]); //转成可以在本地预览的格式
                // 检查是否是图片
                if (!fileFormat.match(/.png|.jpg|.jpeg/)) {
                    flag=false;
                    $("#error_Allmsg").html('上传错误,文件格式必须为：png/jpg/jpeg');
                    return;
                }
                flag=true;
                $('#photo').attr('src', src);
            });
            return flag;
        }

        function checkUserName() {
            var flag;
            var userName = $("#userName").val();
            if (userName == null || userName == "null" || userName == "") {
                flag = false;
                return;
            } else {
                var reg = /^[0-9a-zA-Z_]{8,}$/;
                if (!reg.test(userName)) {
                    flag = false;
                    $("#error_Allmsg").html("用户名至少8位字母数字下划线均可");
                    return;
                } else {
                    $.get("/wenote/user?method=checkUserName", {userName: userName}, function (data) {
                        if (!data.flag) {
                            flag = false;
                            $("#error_Allmsg").html("用户名重复，请检查");
                        } else {
                            flag = true;
                        }
                    });
                }
            }
            return flag;
        }

        function checkNickName() {
            var flag;
            var nickname = $("#nickname").val();
            if (nickname == null || nickname == "null" || nickname == "") {
                flag = false;
                return;
            } else {
                flag=true;
            }
            return flag;
        }

        function checkEmail() {
            var flag;
            var email = $("#email").val();
            if (email == null || email == "null" || email == "") {
                flag = false;
                return;
            } else {
                var reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
                if (!reg.test(email)) {
                    flag = false;
                    $("#error_Allmsg").html("您输入的邮箱，不符合规范，请重新输入");
                    return;
                } else {
                    $.get("/wenote/user?method=checkEmail", {email: email}, function (data) {
                        if (!data.flag) {
                            flag = false;
                            $("#error_Allmsg").html("邮箱已注册过，请检查");
                        } else {
                            flag = true;
                        }
                    });
                }
            }
            return flag;
        }

        function checkBrief(){
            var flag=false;
            var brief=$("#brief").val();
            if(brief==null || brief==""){
                flag=false;
            }else{
                flag=true;
            }
        }

        $(function () {

            $.get("/wenote/user?method=findUser",{},function (data) {
                $("#photo").attr("src",data.photo);
                $("#userName").val(data.userName);
                $("#nickname").val(data.nickname);
                $("#email").val(data.email);
                $("#brief").val(data.brief);
            });

            clear();
            //设置点击事件
            $("#update").click(function () {
                if (checkPassWord() && checkNewPassWord() && checkTwoPassWord()) {
                    $.post("/wenote/user?method=updatePwd", $("#updatePwd").serialize(), function (data) {
                        if (data.flag) {
                            alert("修改密码成功");
                            location.reload();
                        } else {
                            alert("修改密码失败");
                            $("#error_msg").html(data.errorMsg);
                        }
                    });
                }
            });

            $("#saveBtn").click(function () {
                if(checkNickName()  || checkBrief()){
                    $.post("/wenote/user?method=updateUserById",$("#updateMessage").serialize(),function (data) {
                        if (data.flag) {
                            alert("修改信息成功");
                            location.reload();
                        } else {
                            $("#error_Allmsg").html(data.errorMsg);
                        }
                    })
                }else {
                    alert("无要修改的信息");
                }
            });

            $("#password").blur(checkPassWord);
            $("#newpassword").blur(checkNewPassWord);
            $("#twopassword").blur(checkTwoPassWord);

            $("#email").blur(checkEmail);
            $("#nickname").blur(checkNickName);
            $("#brief").blur(checkBrief);



        });
    </script>
</head>
<body>
<div class="m-padding-tb-massive">
    <div class="ui container">
        <div class="ui stackable grid">
            <div class="eleven wide column">
                <div class="five wide column">
                    <div class="ui top attached segment">
                        <h1 class="ui header">个人信息</h1>
                    </div>
                    <div class="ui attached segment">
                        <div id="error_Allmsg" style="color: red; text-align: center;"></div>
                        <form id="updateMessage" action="#" method="post">
                            <p><label data-field="userimage" data-formatter="image">头像</label></p>
                            <img name="photo" id="photo" class="img-circle" src="../images/zfb.jpg" alt="头像"
                                 height="80px;"/>
                            <input id="fileInp" type="file" accept="image/png, image/jpg,image/jpeg,image/gif">
                            <p style="margin-top: 10px;"><label for="userName">用户名</label><input type="text"
                                                                                                  class="form-control"
                                                                                                  name="userName"
                                                                                                  id="userName" readonly></p>
                            <p><label for="nickname">昵称</label><input type="text" class="form-control" name="nickname"
                                                                       id="nickname"></p>
                            <p><label for="email">邮箱</label><input type="text" class="form-control" name="email"
                                                                    id="email"></p>
                            <div class="form-group">
                                <p><label for="brief">个人简介</label><textarea class="form-control" name="brief" id="brief"
                                                                            rows="3"></textarea>
                                </p>
                            </div>
                            <p>
                                <a>
                                    <button id="saveBtn" type="button" class="ui teal button m-mobile-wide">
                                        <i class="edit icon"></i>保存信息
                                    </button>
                                </a>
                                <a href="index.html">
                                    <button type="button" class="ui button">返回</button>
                                </a>
                                <a style="margin-left: 497px;text-decoration: none;"
                                   href="javascript:location.href='/wenote/user?method=exit';"><font
                                        size="4">退出登录</font></a>
                            </p>
                        </form>
                    </div>
                </div>
            </div>
            <div class="five wide column">
                <div class="ui top attached segment">
                    <h1 class="ui header">修改密码</h1>
                </div>
                <form method="post" id="updatePwd" action="#">
                    <div class="ui attached segment">
                        <div id="error_msg" style="color: red; text-align: center;"></div>
                        <p>
                            <label for="password">原密码</label>
                            <input type="password" class="form-control" name="password" id="password"
                                   placeholder="请输入密码">
                        </p>
                    </div>
                    <div class="ui attached segment">
                        <p>
                            <label for="password">新密码</label>
                            <input type="password" class="form-control" name="newpassword" id="newpassword"
                                   placeholder="请输入密码">
                        </p>
                    </div>
                    <div class="ui attached segment">
                        <p>
                            <label for="password">确认密码</label>
                            <input type="password" class="form-control" id="twopassword" placeholder="请输入密码">
                        </p>
                    </div>
                    <div class="ui attached segment">
                        <p>
                            <button id="update" type="button" class="btn btn-default update">修改</button>
                            <button class="btn btn-default update" onclick="clear();">取消</button>
                        </p>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<!--代码放置于</body>上方-->
<script>
    !function () {

        function n(n, e, t) {

            return n.getAttribute(e) || t

        }

        function e(n) {

            return document.getElementsByTagName(n)

        }

        function t() {

            var t = e("script"), o = t.length, i = t[o - 1];

            return {

                l: o, z: n(i, "zIndex", -1), o: n(i, "opacity", .5), c: n(i, "color", "0,0,0"), n: n(i, "count", 99)

            }

        }

        function o() {

            a = m.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,

                c = m.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight

        }

        function i() {

            r.clearRect(0, 0, a, c);

            var n, e, t, o, m, l;

            s.forEach(function (i, x) {

                for (i.x += i.xa, i.y += i.ya, i.xa *= i.x > a || i.x < 0 ? -1 : 1, i.ya *= i.y > c || i.y < 0 ? -1 : 1, r.fillRect(i.x - .5, i.y - .5, 1, 1), e = x + 1; e < u.length; e++) n = u[e],

                null !== n.x && null !== n.y && (o = i.x - n.x, m = i.y - n.y,

                    l = o * o + m * m, l < n.max && (n === y && l >= n.max / 2 && (i.x -= .03 * o, i.y -= .03 * m),

                    t = (n.max - l) / n.max, r.beginPath(), r.lineWidth = t / 2, r.strokeStyle = "rgba(" + d.c + "," + (t + .2) + ")", r.moveTo(i.x, i.y), r.lineTo(n.x, n.y), r.stroke()))

            }),

                x(i)

        }

        var a, c, u, m = document.createElement("canvas"),

            d = t(), l = "c_n" + d.l, r = m.getContext("2d"),

            x = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||

                function (n) {

                    window.setTimeout(n, 1e3 / 45)

                },

            w = Math.random, y = {x: null, y: null, max: 2e4};
        m.id = l, m.style.cssText = "position:fixed;top:0;left:0;z-index:" + d.z + ";opacity:" + d.o, e("body")[0].appendChild(m), o(), window.onresize = o,

            window.onmousemove = function (n) {

                n = n || window.event, y.x = n.clientX, y.y = n.clientY

            },

            window.onmouseout = function () {

                y.x = null, y.y = null

            };

        for (var s = [], f = 0; d.n > f; f++) {

            var h = w() * a, g = w() * c, v = 2 * w() - 1, p = 2 * w() - 1;
            s.push({x: h, y: g, xa: v, ya: p, max: 6e3})

        }

        u = s.concat([y]),

            setTimeout(function () {
                i()
            }, 100)

    }();
</script>
</body>
</html>